package com.zzyl.nursing.controller.member;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.aliyun.oss.common.comm.ServiceClient;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.FamilyMemberDto;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.vo.FamilyMemberFindListVo;
import com.zzyl.nursing.vo.FamilyMemberSelectListVo;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 老人家属Controller
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/member/user")
public class FamilyMemberController extends BaseController
{
    @Autowired
    private IFamilyMemberService familyMemberService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserLoginRequestDto userLoginRequestDto){
        LoginVo loginVo = familyMemberService.login(userLoginRequestDto);
        return success(loginVo);

    }
    @Transactional
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FamilyMemberDto familyMember){
        Long userId = UserThreadLocal.getUserId();
        Long id =familyMemberService.selectElderId(familyMember);

        // 添加空值检查，防止MissingServletRequestParameterException
        String remark = familyMember.getRemark();
        if (remark == null) {
            remark = ""; // 或者根据业务需求设置默认值或抛出异常
        }
        if (id == null) {
            return error("该老人不存在");
        }
        try {
            familyMemberService.insertMember(id, userId, remark);
        }catch (Exception e){
            return error("你已与该老人存在关系");
        }

        return success();
    }
    @GetMapping("/my")
    public AjaxResult my(){
        Long userId = UserThreadLocal.getUserId();
        List<FamilyMemberSelectListVo> familyMemberList = familyMemberService.selectFamilyMember(userId);
        return success(familyMemberList);
    }
    @GetMapping("/list-by-page")
    public AjaxResult listByPage(){
        Long userId = UserThreadLocal.getUserId();
        startPage();
        List<FamilyMemberFindListVo> familyMemberFindListVo = familyMemberService.selectFamilyMemberFindList(userId);
        return success(familyMemberFindListVo);
    }
    @DeleteMapping("/deleteById")
    public AjaxResult cancel(Long id){
        familyMemberService.cancelFamilyMemberById(id);
        return success();
    }


}
